package org.example.library.service;

import org.example.library.Dao.BookDao;
import org.example.library.enmus.BookStatus;
import org.example.library.entity.BookInfo;
import org.example.library.entity.PageRequest;
import org.example.library.entity.PageResult;
import org.example.library.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.util.List;

@Component
public class BookService {
    private static final String SESSION_KEY = "book_session";
    @Autowired
    private BookDao bookDao;
    @Autowired
    private BookMapper bookMapper;
    public List<BookInfo> getList(){
         List<BookInfo> bookInfos=bookDao.mockBookData();
         for (BookInfo info : bookInfos) {
             if (info.getStatus()==1) {
                 info.setStatusCN("可借阅");
             }else {
                 info.setStatusCN("不可借阅");
             }
         }
         return bookInfos;
    }
    public PageResult getListByPage(PageRequest pageRequest){
        Integer count=bookMapper.count();
        List<BookInfo> bookInfos=bookMapper.selectList(pageRequest);
        for (BookInfo bookInfo:bookInfos){
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(count,bookInfos,pageRequest);
    }

    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public Integer deleteBook(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setStatus(BookStatus.DELETED.getCode());
        bookInfo.setId(bookId);
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }
    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.selectById(bookId);
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        return bookMapper.batchDeleteBook(ids);
    }
}
